import 'package:ff_navigation_bar_plus/ff_navigation_bar_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:meal/components/home/home.dart';
import 'package:meal/constant/antd_icons.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/event/event_model.dart';
import 'package:meal/route/route.dart';
import 'package:meal/utils/colors.dart';
import 'package:meal/utils/sp.dart';

class HomeView extends StatefulWidget {
  const HomeView({super.key});

  @override
  State<HomeView> createState() => _HomeViewState();
}

class _HomeViewState extends State<HomeView> {
  int selectedIndex = 0;

  final PageController pageController = PageController();

  final List<Widget> pageList = [
    const IndexPage(),
    const UnfinishedOrder(),
    const FinishOrder(),
    const MinePage()
  ];

  @override
  void initState() {
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
      statusBarIconBrightness: Brightness.light,
      statusBarColor: Colors.transparent,
    ));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: createPageView(),
      bottomNavigationBar: FFNavigationBar(
        theme: FFNavigationBarTheme(
          barBackgroundColor: Colors.white,
          selectedItemBorderColor: Colors.transparent,
          selectedItemBackgroundColor: ColorsUtils.rgba(255, 110, 83, 1),
          selectedItemIconColor: Colors.white,
          selectedItemLabelColor: ColorsUtils.rgba(255, 110, 83, 1),
          showSelectedItemShadow: false,
          barHeight: Sizes.default60,
        ),
        selectedIndex: selectedIndex,
        onSelectTab: (index) {
          if (index == 3) {
            bool? isLogin = SpUtils.getBool('is_login');
            if (isLogin == false) {
              return Get.toNamed(RouteGet.userLoginWelcome);
            }
          }
          selectedIndex = index;
          pageController.jumpToPage(index);
          globalBus.fire(ClosedOverlayEntryEvent());
          setState(() {});
        },
        items: [
          FFNavigationBarItem(
            iconData: AntdIcons.home,
            label: '首页',
          ),
          FFNavigationBarItem(
            iconData: AntdIcons.orderedList,
            label: '未完成订单',
          ),
          FFNavigationBarItem(
            iconData: AntdIcons.outdent,
            label: '历史订单',
          ),
          FFNavigationBarItem(
            iconData: AntdIcons.user,
            label: '我的',
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    pageController.dispose();
    super.dispose();
  }

  PageView createPageView() {
    return PageView(
      physics: const NeverScrollableScrollPhysics(),
      scrollBehavior: const ScrollBehavior(),
      controller: pageController,
      children: pageList,
    );
  }
}
